package com.gym.manager.interfaz;

import com.gym.manager.mundo.Rutina;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

public class ListaRutinas extends List implements CommandListener {

    private int modo;
    private Command continuar;
    private Command volver;
    private MIDletGymManager principal;
    private Vector rutinas;

    public ListaRutinas(int modo, Vector rutinas, MIDletGymManager midlet) {
        super("Select workout", List.EXCLUSIVE);
        setCommandListener(this);
        principal = midlet;

        continuar = new Command("Select", Command.SCREEN, 0);
        addCommand(continuar);

        volver = new Command("Volver", Command.SCREEN, 0);
        addCommand(volver);
        this.rutinas = rutinas;
        this.modo = modo;
        for (int i = 0; i < this.rutinas.size(); i++) {
            Rutina actual = (Rutina) rutinas.elementAt(i);
            append(actual.getNombre(), null);
        }
        setSelectedIndex(0, true);
    }

    public void commandAction(Command c, Displayable d) {
        if (d == this) {
            if (c == continuar) {

                int index = this.getSelectedIndex();
                Rutina param = (Rutina) rutinas.elementAt(index);
                if (modo == 1) {
                    principal.mostrarFormaEditarRutina(new Alert("Info", "Workout: " + param.toString() + " selected", null, AlertType.INFO), param);
                } else if (modo == 0) {
                    principal.eliminarRutina(param);
                    principal.mostrarFormaPrincipal(new Alert("Info", "Workout deleted", null, AlertType.INFO));

                }

            }
            if (c == volver) {
                principal.mostrarFormaPrincipal(null);
            }
        }
    }
}
